<template>
  <el-dialog
    v-if="showDialog"
    :title="showTitle"
    :visible="showDialog"
    @close="btnCancel"
    @closed="closed"
  >
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form
      ref="deptForm"
      label-width="120px"
      :model="formData"
      :rules="rules"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="formData.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="formData.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="formData.manager"
          style="width: 80%"
          placeholder="请选择"
          @focus="getManager"
        >
          <el-option
            v-for="item in managers"
            :key="item.id"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import {
  addDepartments,
  editDepartments,
  getDepartDetail,
  getDepartments
} from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  name: 'AddDept',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      default: null
    }
  },
  data() {
    const nameValidate = async(rule, value, callback) => {
      if (value) {
        const { depts } = await getDepartments()
        if (this.formData.id) {
          depts.some(
            item => item.name === value && item.id === this.formData.id
          )
            ? callback()
            : callback(new Error(`同级以存在名称为${value}的部门`))
        } else {
          // 新增模式
          depts.some(item => item.name === value).isRepeat
            ? callback(new Error(`同级以存在名称为${value}的部门`))
            : callback()
        }
      }
    }
    const codeValidate = async(rule, value, callback) => {
      if (value) {
        const { depts } = await getDepartments()
        if (this.formData.id) {
          depts.some(
            item => item.code === value && item.id === this.formData.id
          )
            ? callback()
            : callback(new Error(`同级以存在名称为${value}的部门`))
        } else {
          // 新增模式
          depts.some(item => item.code === value)
            ? callback(new Error(`同级以存在编码为${value}的部门`))
            : callback()
        }
      }
    }

    return {
      managers: [],
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '长度为1-50个字符', trigger: 'blur' },
          { validator: nameValidate, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '长度为1-50个字符', trigger: 'blur' },
          { validator: codeValidate, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门编码不能为空', trigger: 'change' }
        ],
        introduce: [
          { min: 1, max: 300, message: '长度为1-300个字符', trigger: 'blur' },
          { required: true, message: '部门介绍不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    btnOK() {
      this.$refs.deptForm.validate(async isOK => {
        if (isOK) {
          // 通过表单校验后
          if (this.formData.id) {
            await editDepartments(this.formData)
          } else {
            await addDepartments({ ...this.formData, pid: this.treeNode.id })
          }
          this.$emit('addDepts')
          this.$emit('update:showDialog', false)
          this.$message.success(this.showTitle + '成功')
        }
      })
    },
    btnCancel() {
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      this.$emit('update:showDialog', false)
      this.$refs.deptForm.resetFields()
    },
    closed() {
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
    },
    async getManager() {
      this.managers = await getEmployeeSimple()
    },
    async getDepartDetail(id) {
      this.formData.id = id
      this.formData = await getDepartDetail(id)
    }
  }
}
</script>

<style></style>
